#include "Bar.h"
int ProgressBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    RECT rect;
    GetClientRect(&rect);
    Rect rc(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
    rect2_ = rc;
    rc.Inflate(-1, -1);
    rect1_ = rc;
    rc.Inflate(-1, -1);
    rect0_ = rc;
    pbrush_ = new LinearGradientBrush(rect0_, Color(230, 77, 77), Color(188, 31, 31), LinearGradientModeVertical);
    repeaterBrush = new LinearGradientBrush(rect0_, Color(255, 203, 127), Color(214, 112, 0), LinearGradientModeVertical);
    return TRUE;

}
void ProgressBar::Update(PlayerState& ps)
{        
    //DrawRectangle(player_state_.curentPos, ps.curentPos, ps.length, ps.currentState);
    DrawRectangle(0, ps.curentPos, ps.length, ps.currentState);
    player_state_ = ps;
}
void ProgressBar::OnPaint(CDCHandle dc)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(&ps);

    Graphics gfx(hdc);
    LinearGradientBrush br(rect2_, Color(10, 10, 10), Color(66, 66, 66), LinearGradientModeVertical);
    gfx.FillRectangle(&br, rect2_);

    SolidBrush p(Color(25, 25, 25));
    gfx.FillRectangle(&p, rect1_);

    SolidBrush p2(Color(68, 68, 68));
    gfx.FillRectangle(&p2, rect0_);

    EndPaint(&ps);
}
LRESULT ProgressBar::OnDestroy(){
    PostQuitMessage(0);
    delete pbrush_;
    delete repeaterBrush;
    return 0;
} 

void ProgressBar::DrawRectangle(long start, long end, long total, int state){
    Rect rect = rect0_;
    rect.X  +=  rect.Width * (double)start / total;
    rect.Width *= (double)(end - start) / total;

    HDC hdc = GetDC();
    Graphics gfx(hdc);
    if(state == PlayerState::repeate)
        gfx.FillRectangle(repeaterBrush, rect);
    if(state == PlayerState::play)
        gfx.FillRectangle(pbrush_, rect );
    ReleaseDC(hdc);
}
